function Viewpoints(){
  this.uri = null;
  this.viewpoints = new Array();
};

Viewpoints.prototype.toXml = function() {
  var xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  xmlStr += "<!DOCTYPE entity SYSTEM \"" + VIEWPOINTS_DTD + "\">\n";
  xmlStr += "<viewpoints>\n";
  for(i = 0; i < this.viewpoints.length; i++){
    var href, name;
    if ('undefined' == typeof this.viewpoints[i].href)
      href = "";
    else
      href = this.viewpoints[i].href + "";
    if ('undefined' == typeof this.viewpoints[i].name)
      name = "";
    else
      name = this.viewpoints[i].name + "";
    xmlStr += "<viewpoint href=\"" + href.toXml() + "\">" + name.toXml() + "</viewpoint>";
  }
  xmlStr += "</viewpoints>";
  return xmlStr;
}

Viewpoints.prototype.parseXml = function(xmlStr){
  var vps = new Viewpoints();
  var xmlReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
                          .createInstance(Components.interfaces.nsISAXXMLReader);
  xmlReader.contentHandler = {
    currentElement: null,
    tmpViewpoint: null,

    startDocument: function() {},
		endDocument: function() {},

    startElement: function(uri, localName, qName, /*nsISAXAttributes*/ attributes) {
      this.currentElement = qName;

      if(qName == "viewpoint"){
        this.tmpViewpoint = {};
        for(var i=0; i<attributes.length; i++)
          if(attributes.getQName(i) == "href")
            this.tmpViewpoint.href = attributes.getValue(i);
      }
    },

    endElement: function(uri, localName, qName) {
      //debug("[endElement] currentElement:" + this.currentElement + ", typeof:" + (typeof this.tmpTopic),1);
      if(this.currentElement == "viewpoint" && this.tmpViewpoint != null){
        vps.viewpoints.push(this.tmpViewpoint);
        this.tmpViewpoint = null;
        //debug("[endElement] vps.viewpoints.push:" + vps.viewpoints.length ,1);
      }
      this.currentElement = null;
    },

    characters: function(value) {
      if((typeof this.currentElement) != "string")
        return;
      if(this.currentElement == "viewpoint"){
        this.tmpViewpoint.name = value;
        //debug("[startElement] this.tmpViewpoint.name:" + this.tmpViewpoint.name ,1);
      }
    },
    processingInstruction: function(target, data) {},
    ignorableWhitespace: function(whitespace) {},
    startPrefixMapping: function(prefix, uri) {},
    endPrefixMapping: function(prefix) {},

    // nsISupports
    QueryInterface: function(iid) {
      if(!iid.equals(Components.interfaces.nsISupports) &&
         !iid.equals(Components.interfaces.nsISAXContentHandler))
        throw Components.results.NS_ERROR_NO_INTERFACE;
      return this;
    }
  }
  xmlReader.parseFromString(xmlStr, "text/xml");
  return vps;
}

Viewpoints.prototype.httpGet = function(user,pass) {
	if(this.uri == null) return null;

	var req = new customRequest('GET', this.uri, "");
	var http = new HTTPCore();
	if(typeof user == "string" && user != "")
	  http.auth(user, pass);

	var res = http.doOperation(req);
	if(res == false) return false;
	debug("[viewpoints.js::httpGet] Viewpoint.httpGet URL:" + this.uri, 1);
	debug("[viewpoints.js::httpGet] Viewpoint.httpGet Response:" + http.response, 1);
	var res = this.parseXml(http.response);
	return res;
}